﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
            var sqlstu = "select * from Articles";
            var dt = DebHelper.GetDataTable(sqlstu);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = true;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Articles where Title like '%{0}%'", name);
            var dt = DebHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            EditFrom editForm = new EditFrom();

            var res = editForm.ShowDialog();

            if (res == DialogResult.Yes)
            {
                MessageBox.Show("你成功添加！！！", "提示");
            }
            else
            {
                MessageBox.Show("你取消了添加！！！", "提示");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;
                var res = MessageBox.Show("确定删除吗？？？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {

                    var sql = string.Format("delete from Articles where Id={0}", id);
                    var rowCount = DebHelper.AddOrUpdateOrDelect(sql);
                    if (rowCount > 0)
                    {
                        var sqlstu = "select * from Articles";
                        var dt = DebHelper.GetDataTable(sqlstu);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功！！！！");


                    }
                }
            }
            else
            {
                MessageBox.Show("没有选择任何数据无法删除！！！！");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
                        if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var createdTime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;

                EditFrom editForm =new EditFrom(id, title, author, createdTime);
                editForm.ShowDialog();


            }
            else
            {
                MessageBox.Show("你没有选择任何行！！！", "提示");
            }

        }
    }
}
